<?php

namespace app\store\controller\content;

use app\common\model\Ticket;
use app\store\controller\Controller;
use app\store\model\Introduce;
use app\store\model\article\Category as CategoryModel;
use app\store\model\store\Scenic;

/**
 * 文章管理控制器
 * Class article
 * @package app\store\controller\content
 */
class Article extends Controller
{
    /**
     * 总列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        
        $model = new Introduce();
        
        $list = $model->getList();
        
        return $this->fetch('index', compact('list'));
    }
    /**
     * 美食列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function food()
    {
        $model = new Introduce();
        $list = $model->where('is_delete', '=', 0)
            ->where('category_id','=',1)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);;
            
        return $this->fetch('index', compact('list'));
    }

    /**
     * 民宿列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function homestay()
    {
        $model = new Introduce();
        $list = $model->where('is_delete', '=', 0)
            ->where('category_id','=',2)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);;
        
        return $this->fetch('index', compact('list'));
    }

    /**
     * 景点列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function scenic()
    {
        $model = new Introduce();
        $list = $model->where('is_delete', '=', 0)
            ->where('category_id','=',3)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);;
        
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加文章
     * @return array|mixed
     */
    public function add()
    {
        $model = new Introduce();
        if (!$this->request->isAjax()) {
            //景区
            $model2 = new Scenic();
            $scenic = $model2->getList();
            // 文章分类
            $catgory = CategoryModel::getAll();
            //商品列表
            $ticket = Ticket::where('is_delete','=',0)
                    ->where('status','=',10)
                    ->select();
            return $this->fetch('add', compact('catgory','scenic','ticket'));
        }
        // 新增记录
        
        $data = $this->postData('article');
        
        if(isset($data['ticket_id'])){
            $data['ticket_id'] = implode(',',$data['ticket_id']);
        }
        
        if ($model->add($data)) {
            return $this->renderSuccess('添加成功', url('content.article/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新文章
     * @param $article_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        // 文章详情
        $model = Introduce::detail($id);
        if (!$this->request->isAjax()) {
            //景区
            $scenic = new Scenic();
            $list = $scenic->getList();
            // 文章分类
            $catgory = CategoryModel::getAll();
            //商品列表
            $ticket = Ticket::where('is_delete','=',0)
                    ->where('status','=',10)
                    ->select();
            if(isset($model['ticket_id'])){
                $model['ticket_id'] = explode(',',$model['ticket_id']);
            }
            
            return $this->fetch('edit', compact('model','catgory','list','ticket'));
        }
        // 更新记录
        $data = $this->postData('article');
        if(isset($data['ticket_id'])){
            $data['ticket_id'] = implode(',',$data['ticket_id']);
        }
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功', url('content.article/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除文章
     * @param $article_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($id)
    {
        // 文章详情
        $model = Introduce::detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 修改文章状态
     * @param $id
     * @param boolean $state
     * @return array
     */
    public function state($id, $state)
    {

        $model = Introduce::get($id);       
        $model->status = $state == 0 ? 1 : 0;
        if (!$model->save()) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 是否横幅展示
     * @param $id
     * @param boolean $state
     * @return array
     */
    public function banner($id, $state)
    {

        $model = Introduce::get($id);       
        $model->is_banner = $state == 0 ? 1 : 0;
        if (!$model->save()) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
}